<template>
    <div class="default-main ba-table-box">
        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('shop.order.export.quick Search Fields') })"
        ></TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        <Table ref="tableRef"></Table>

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import { baTableApi } from '/@/api/common'
import { defaultOptButtons } from '/@/components/table'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { getDownloadUrl, retryTask } from '/@/api/backend/shop/order/export'
import { showDownloadButton } from './index'
import { showRetryButton } from '/@/views/backend/shop/order/export/index'
import { ElNotification } from 'element-plus'

defineOptions({
    name: 'shop/order/export',
})

const { t } = useI18n()
const tableRef = ref()

let optButtons: OptButton[] = []

optButtons = optButtons.concat(defaultOptButtons(['delete']))

optButtons = optButtons.concat([
    {
        render: 'confirmButton',
        name: 'detail',
        title: 'shop.order.export.download',
        text: '',
        type: 'success',
        icon: 'fa fa-download',
        popconfirm: {
            confirmButtonText: t('Confirm'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            width: '120px',
            title: '确认要下载导出文件吗?',
        },
        click: (row) => {
            const downloadUrl = getDownloadUrl(row.export_file_path)
            window.location.href = downloadUrl
        },
        display: (row) => {
            return showDownloadButton(row)
        },
    },
])

optButtons = optButtons.concat([
    {
        render: 'confirmButton',
        name: 'retry_export',
        title: 'shop.order.export.retry_export',
        text: '',
        type: 'warning',
        icon: 'fa fa-refresh',
        popconfirm: {
            confirmButtonText: t('Confirm'),
            cancelButtonText: t('Cancel'),
            confirmButtonType: 'danger',
            width: '120px',
            title: '确认要重新运行导出任务吗?',
        },
        click: (row) => {
            baTable.table.loading = true
            retryTask({
                id: row.id,
            })
                .then((res) => {
                    setTimeout(() => {
                        baTable.table.loading = false
                        baTable.onTableHeaderAction('refresh', {})
                        ElNotification({
                            type: 'success',
                            message: res.msg,
                        })
                    }, 1500)
                })
                .catch((err) => {
                    console.log(err)
                    baTable.table.loading = false
                })
        },
        display: (row) => {
            return showRetryButton(row)
        },
    },
])

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/shop.order.Export/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            {
                label: t('shop.order.export.id'),
                prop: 'id',
                align: 'center',
                width: 70,
                operator: 'RANGE',
                sortable: 'custom',
            },
            {
                label: t('shop.order.export.admin_nickname'),
                prop: 'admin.nickname',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                minWidth: 180,
                showOverflowTooltip: true,
            },
            {
                label: t('shop.order.export.name'),
                prop: 'name',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: 'LIKE',
                sortable: false,
                minWidth: 150,
                showOverflowTooltip: true,
            },
            {
                label: t('shop.order.export.export_fields'),
                prop: 'export_fields',
                align: 'center',
                operator: false,
                sortable: false,
                operatorPlaceholder: t('Fuzzy query'),
                showOverflowTooltip: {
                    placement: 'top-start',
                },
                minWidth: 200,
            },
            {
                label: t('shop.order.export.num'),
                prop: 'query_params.limit',
                align: 'center',
                operatorPlaceholder: t('Fuzzy query'),
                operator: false,
                sortable: false,
                width: 100,
            },
            {
                label: t('shop.order.export.time_range'),
                prop: 'time_range',
                align: 'center',
                operator: false,
                sortable: false,
                width: 220,
                className: 'time-range',
            },
            {
                label: t('shop.order.export.status'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                operator: 'eq',
                sortable: false,
                replaceValue: {
                    0: t('shop.order.export.status 0'),
                    1: t('shop.order.export.status 1'),
                    2: t('shop.order.export.status 2'),
                    3: t('shop.order.export.status 3'),
                },
                custom: {
                    0: 'info',
                    1: '',
                    2: 'success',
                    3: 'danger',
                },
                width: 100,
            },
            {
                label: t('shop.order.export.last_export_time'),
                prop: 'last_export_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('shop.order.export.create_time'),
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                operator: 'RANGE',
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('shop.order.export.export_finish_time'),
                prop: 'export_finish_time',
                align: 'center',
                render: 'datetime',
                operator: false,
                sortable: 'custom',
                width: 160,
                timeFormat: 'yyyy-mm-dd hh:MM:ss',
            },
            {
                label: t('Operate'),
                align: 'center',
                width: 120,
                render: 'buttons',
                buttons: optButtons,
                operator: false,
                fixed: 'right',
            },
        ],
        dblClickNotEditColumn: ['all'],
        showComSearch: true,
    },
    {
        defaultItems: { export_fields: [] },
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})
</script>

<style lang="scss" scoped>
:deep(.el-alert__content .el-alert__title) {
    white-space: pre;
}

:deep(.time-range) {
    .cell {
        white-space: pre;
    }
}
</style>
